<?php

namespace app\admin\controller\service\order;

use app\common\controller\Backend;
use think\Db;
use think\Exception;

/**
 * 投诉管理
 *
 * @icon fa fa-circle-o
 */
class Complaint extends Backend
{

    /**
     * Complaint模型对象
     * @var \app\admin\model\service\order\Complaint
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\service\order\Complaint;
        $this->view->assign("stateList", $this->model->getStateList());
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as &$val)
        {
            $val->user = db('user')->where('id',$val->user_id)->field('avatar,nickname,mobile')->find();
            $order = db('service_order')->where(['id'=>$val->order_id])->find();
            $val->address = db('service_order_address')->where('order_id',$val->order_id)->field('name,sex,mobile,province,city,district,area,address')->find();
            $val->detail = db('service_order_detail')->where('order_id',$val->order_id)->field('name,image,sku_name,num,price,sumprice')->find();
            $val->skill = $order['skill_id']?db('service_skill')->field('name,mobile,image')->where('id',$order['skill_id'])->find():'';
            $val->shop = $order['shop_id']?db('service_shop')->where('id',$order['shop_id'])->field('name,abbr,logo_image,leader_name,leader_mobile')->find():'';
            $val->order = $order;
            $val->create_time = date("Y-m-d H:i",$val->createtime);
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function handle()
    {
        $get = input('get.');
        $row = $this->model->get($get['ids']);
        $row->state != 0 && $this->error('请勿重复审核');
        $this->model->where('id',$get['ids'])->update(['state'=>$get['state']]);
        $order = \app\api\model\service\Order::where('id',$row->order_id)->field('id,user_id,shop_id,skill_id')->find();
        \app\api\model\service\OrderLog::create(['order_id'=>$row->order_id,'user_id'=>$row->user_id,'type'=>18,'content'=>'订单投诉已处理']);
        $userOpenid = \app\api\model\service\UserInfo::getOpenid($order['user_id'],0);
        $content = explode(',',$row->complaint_content);
        \app\api\model\service\Order::createComplaintTemplateParams(['content'=>$content[0],'id'=>$order['id'],'time'=>$row->createtime,'res'=>'已处理','note'=>'已处理','state'=>$get['state'],'type'=>0,'templateAttr'=>'user_complaint_template','openid'=>$userOpenid]);
        $this->success('投诉已处理');
    }

    public function deduct($ids)
    {
        $row = \app\admin\model\service\order\Order::get($ids);
        $row->skill_ensure_price = $row->skill_id ? \app\api\model\service\Skill::where('id',$row->skill_id)->value('ensure_price'):'';
        $row->shop_ensure_price = $row->shop_id ? \app\api\model\service\Shop::where('id',$row->shop_id)->value('ensure_price'):'';
        if($this->request->isPost())
        {
            $params = $this->request->post('row/a');
            Db::startTrans();
            try{
                if(isset($params['skill_ensure']) && $params['skill_ensure']>0) {
                    $params['skill_ensure']>$row->skill_ensure_price && $this->error('保证金不足,无法扣除');
                    $skill = \app\api\model\service\Skill::where('id', $params['skill_id'])->field('user_id,id,ensure_price')->find();
                    \app\api\model\service\Skill::money(-$params['skill_ensure'], $skill['user_id'], $params['skill_note']);
                    \app\api\model\service\SkillEnsurePay::reduceSkillEnsure($skill['id']);
                }
                if(isset($params['shop_ensure']) && $params['shop_ensure']>0) {
                    $params['shop_ensure']>$row->shop_ensure_price && $this->error('保证金不足,无法扣除');
                    $shop = \app\api\model\service\Shop::where('id', $params['shop_id'])->field('user_id,id,ensure_price')->find();
                    \app\api\model\service\Shop::money(-$params['shop_ensure'], $shop['user_id'], $params['shop_note']);
                    \app\api\model\service\ShopEnsurePay::reduceShopEnsure($shop['id']);
                }
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('保证金扣除失败',$e->getMessage());
            }
            $this->success('保证金已扣除');
        }
        $this->assign(['row'=>$row]);
        return $this->view->fetch();
    }




}
